package com.szc.demo01.controller.base;

/**
 * @Author: ShiZhiCheng
 * @Date: 2021/01/16 10:22:58
 */

import com.szc.demo01.exception.NotBossException;
import com.szc.demo01.pojo.Emp;
import com.szc.demo01.pojo.User;
import com.szc.demo01.service.AuthorityService;
import com.szc.demo01.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

public abstract class BaseController{

    @Resource
    EmpService empService;

    @Resource
    AuthorityService authorityService;

    @Autowired
    HttpServletRequest httpServletRequest;

    protected User getUser() {
        return (User) httpServletRequest.getSession().getAttribute("user");
    }

    protected long getUserId() {
        return this.getUser().getId();
    }

    protected void assertUserIdHasPerm(int authority) throws NotBossException {
        Emp empServiceById = empService.getById(getUserId());
        // 获得拥有权限
        String hasAuthoritys = empServiceById.getAuthoritys();

        String tAuthority = String.valueOf(authority);
        if (!hasAuthoritys.contains(tAuthority)){
            //未拥有权限
            if (authority == 1){
                //查看信息权限
                throw new NotBossException(authorityService.getById(1).getAException());
            }else if (authority == 2){
                //查看文件权限
                throw new NotBossException(authorityService.getById(2).getAException());
            }else if (authority == 3){
                //查看公告权限
                throw new NotBossException(authorityService.getById(3).getAException());
            }else if (authority == 4){
                //查看公告权限
                throw new NotBossException(authorityService.getById(4).getAException());
            }else if (authority == 5){
                //查看行政权限
                throw new NotBossException(authorityService.getById(5).getAException());
            }else if (authority == 6){
                //查看行政权限
                throw new NotBossException(authorityService.getById(6).getAException());
            }
        }
    }

    protected boolean userIdHasPerm(int authority){
        Emp empServiceById = empService.getById(getUserId());
        // 获得拥有权限
        String hasAuthoritys = empServiceById.getAuthoritys();
        String tAuthority = String.valueOf(authority);

        //拥有权限
        return hasAuthoritys.contains(tAuthority);
    }

}
